/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Codigo;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author kyerte
 */
public class FrmPromotores extends javax.swing.JFrame {

    private ConsultaPromotor consulta_Promotor;
    private List<Promotor> resultado_Promotores;
    private Promotor promotor_Actual;
    private int numero_Promotores;
    private int indiceActual;
    private int id_Promotor;
    private boolean nuevo_Promotor;

    public FrmPromotores() {
        initComponents();
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagenes/user_32.png")).getImage());
        this.getContentPane().setBackground(new java.awt.Color(244, 243, 176));
        consulta_Promotor = new ConsultaPromotor();
        
        explorarPromotores();
        
        txtNombre.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (txtNombre.getText().length() == 45) {
                    e.consume();
                }
            }

            @Override
            public void keyPressed(KeyEvent arg0) {
            }

            @Override
            public void keyReleased(KeyEvent arg0) {
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNombre = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        cmdCancelar = new javax.swing.JButton();
        cmdEliminar = new javax.swing.JButton();
        cmdNuevo = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cmdUltimo = new javax.swing.JButton();
        cmdGuardar = new javax.swing.JButton();
        lblTotal = new javax.swing.JLabel();
        cmdAnterior = new javax.swing.JButton();
        cmdPrimero = new javax.swing.JButton();
        txtIndiceActual = new javax.swing.JTextField();
        cmdSiguiente = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Promotores");
        setResizable(false);

        lblNombre.setFont(new java.awt.Font("Ubuntu", 2, 18)); // NOI18N
        lblNombre.setText("Nombre");

        txtNombre.setFont(new java.awt.Font("Ubuntu", 2, 18)); // NOI18N

        cmdCancelar.setBackground(new java.awt.Color(255, 255, 255));
        cmdCancelar.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Cancelar.png"))); // NOI18N
        cmdCancelar.setMnemonic('c');
        cmdCancelar.setText("Cancelar");
        cmdCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelarActionPerformed(evt);
            }
        });

        cmdEliminar.setBackground(new java.awt.Color(255, 255, 255));
        cmdEliminar.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Borrar.png"))); // NOI18N
        cmdEliminar.setMnemonic('e');
        cmdEliminar.setText("Eliminar");
        cmdEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEliminarActionPerformed(evt);
            }
        });

        cmdNuevo.setBackground(new java.awt.Color(255, 255, 255));
        cmdNuevo.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Nuevo.png"))); // NOI18N
        cmdNuevo.setMnemonic('n');
        cmdNuevo.setText("Nuevo");
        cmdNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNuevoActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu", 0, 18)); // NOI18N
        jLabel1.setText("/");

        cmdUltimo.setBackground(new java.awt.Color(255, 255, 255));
        cmdUltimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Forward.png"))); // NOI18N
        cmdUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdUltimoActionPerformed(evt);
            }
        });

        cmdGuardar.setBackground(new java.awt.Color(255, 255, 255));
        cmdGuardar.setFont(new java.awt.Font("Ubuntu", 2, 15)); // NOI18N
        cmdGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Guardar.png"))); // NOI18N
        cmdGuardar.setMnemonic('g');
        cmdGuardar.setText("Guardar");
        cmdGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdGuardarActionPerformed(evt);
            }
        });

        lblTotal.setFont(new java.awt.Font("Ubuntu", 0, 24)); // NOI18N
        lblTotal.setText("100");

        cmdAnterior.setBackground(new java.awt.Color(255, 255, 255));
        cmdAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Back.png"))); // NOI18N
        cmdAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAnteriorActionPerformed(evt);
            }
        });

        cmdPrimero.setBackground(new java.awt.Color(255, 255, 255));
        cmdPrimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/First.png"))); // NOI18N
        cmdPrimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdPrimeroActionPerformed(evt);
            }
        });

        txtIndiceActual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIndiceActualActionPerformed(evt);
            }
        });

        cmdSiguiente.setBackground(new java.awt.Color(255, 255, 255));
        cmdSiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Last.png"))); // NOI18N
        cmdSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSiguienteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblNombre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(cmdPrimero)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdAnterior)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtIndiceActual, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addGap(9, 9, 9)
                        .addComponent(lblTotal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdSiguiente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdUltimo))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cmdNuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdCancelar)
                        .addGap(0, 340, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cmdAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(lblTotal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(cmdPrimero, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cmdUltimo, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cmdSiguiente, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(txtIndiceActual, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblNombre)
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cmdGuardar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(cmdEliminar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(cmdCancelar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void explorarPromotores() {
        resultado_Promotores = consulta_Promotor.consultarPromotor();
        numero_Promotores = resultado_Promotores.size();

        if (numero_Promotores != 0) {

            indiceActual = 0;

            promotor_Actual = resultado_Promotores.get(indiceActual);
            id_Promotor = promotor_Actual.obtenerIdPromotor();
            txtNombre.setText(promotor_Actual.obtenerNombre());

            txtIndiceActual.setText("" + (indiceActual + 1));
            lblTotal.setText("" + numero_Promotores);

        } else {
            cmdGuardar.setEnabled(false);
            cmdCancelar.setEnabled(false);
            cmdEliminar.setEnabled(false);
        }
    }

    private void cmdNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNuevoActionPerformed
        nuevo_Promotor = true;
        txtNombre.setText("");
        cmdGuardar.setEnabled(true);
        cmdCancelar.setEnabled(true);
        cmdEliminar.setEnabled(false);
    }//GEN-LAST:event_cmdNuevoActionPerformed

    private void cmdGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdGuardarActionPerformed
        cmdGuardar.setEnabled(true);
        cmdCancelar.setEnabled(true);

        String mensaje = "";

        int resultado = 0;

        if (nuevo_Promotor) {
            resultado = consulta_Promotor.agregarNuevoPromotor(txtNombre.getText());
            mensaje = "Se agregó nuevo promotor";
        } else {
            resultado = consulta_Promotor.actualizarPromotor(txtNombre.getText(), id_Promotor);
            mensaje = "Se actualizó promotor";
        }

        if (resultado == 1) {
            JOptionPane.showMessageDialog(null, mensaje, "Promotores",
                    JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "No se pudo realizar la operación",
                    "Promotores", JOptionPane.ERROR_MESSAGE);
        }

        nuevo_Promotor = false;
        explorarPromotores();

        cmdNuevo.setEnabled(true);
        cmdEliminar.setEnabled(true);
    }//GEN-LAST:event_cmdGuardarActionPerformed

    private void cmdEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEliminarActionPerformed
        int opc = JOptionPane.showConfirmDialog(this, "¿Seguro que deseas "
                + "eliminar este promotor?",
                "Promotores", JOptionPane.YES_NO_OPTION);

        int resultado = 0;

        if (opc == JOptionPane.YES_OPTION) {


            resultado = consulta_Promotor.eliminarPromotor(id_Promotor);

            if (resultado != 0) {

                JOptionPane.showMessageDialog(null, "Promotor eliminado",
                        "Promotores", JOptionPane.INFORMATION_MESSAGE);

                explorarPromotores();

            } else {
                JOptionPane.showMessageDialog(null, "No se pudo eliminar promotor",
                        "Promotores", JOptionPane.INFORMATION_MESSAGE);
            }
        }

        cmdNuevo.setEnabled(true);
        cmdGuardar.setEnabled(true);
        cmdCancelar.setEnabled(true);
        cmdEliminar.setEnabled(true);
    }//GEN-LAST:event_cmdEliminarActionPerformed

    private void cmdCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelarActionPerformed
        explorarPromotores();
        nuevo_Promotor = false;
        cmdNuevo.setEnabled(true);
        cmdGuardar.setEnabled(true);
        cmdCancelar.setEnabled(true);
        cmdEliminar.setEnabled(true);
    }//GEN-LAST:event_cmdCancelarActionPerformed

    private void cmdPrimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdPrimeroActionPerformed
        indiceActual = 0;
        txtIndiceActual.setText("" + (indiceActual + 1));
        txtIndiceActualActionPerformed(evt);
    }//GEN-LAST:event_cmdPrimeroActionPerformed

    private void txtIndiceActualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIndiceActualActionPerformed
        String str = txtIndiceActual.getText();

        if (ValidaNumeros.validarNumeroEntero(str)) {

            indiceActual = (Integer.parseInt(str) - 1);

            if (numero_Promotores != 0 && indiceActual < numero_Promotores) {

                promotor_Actual = resultado_Promotores.get(indiceActual);
                id_Promotor = promotor_Actual.obtenerIdPromotor();
                txtNombre.setText(promotor_Actual.obtenerNombre());
            }
        } else {
            JOptionPane.showMessageDialog(null, "Ingresa un número válido",
                    "Promotores", JOptionPane.ERROR_MESSAGE);
            explorarPromotores();
        }
    }//GEN-LAST:event_txtIndiceActualActionPerformed

    private void cmdAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAnteriorActionPerformed
        indiceActual--;

        if (indiceActual < 0) {
            indiceActual = numero_Promotores - 1;
        }

        txtIndiceActual.setText("" + (indiceActual + 1));
        txtIndiceActualActionPerformed(evt);
    }//GEN-LAST:event_cmdAnteriorActionPerformed

    private void cmdSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSiguienteActionPerformed
        indiceActual++;

        if (indiceActual >= numero_Promotores) {
            indiceActual = 0;
        }

        txtIndiceActual.setText("" + (indiceActual + 1));
        txtIndiceActualActionPerformed(evt);
    }//GEN-LAST:event_cmdSiguienteActionPerformed

    private void cmdUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdUltimoActionPerformed
        indiceActual = numero_Promotores - 1;

        txtIndiceActual.setText("" + (indiceActual + 1));

        txtIndiceActualActionPerformed(evt);
    }//GEN-LAST:event_cmdUltimoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdAnterior;
    private javax.swing.JButton cmdCancelar;
    private javax.swing.JButton cmdEliminar;
    private javax.swing.JButton cmdGuardar;
    private javax.swing.JButton cmdNuevo;
    private javax.swing.JButton cmdPrimero;
    private javax.swing.JButton cmdSiguiente;
    private javax.swing.JButton cmdUltimo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JTextField txtIndiceActual;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
